<template>
	<om-page title="我">
		<view v-if="hasLogin">
			<uni-list>
				<uni-list-item :showArrow="false" class="bg-steelblue" :showHover="false">
					<view slot="header">
						<view class="cu-avatar xl round margin-left"
							:style="'background-image:url(\''+ om.getAvatar() +'\');'"></view>
					</view>
					<view class="tui-flex">
						<view class="tui-col-4 text-bold text-white" style="margin-left: 10px;">{{ userName }}</view>
						<view class="tui-col-4"></view>
						<view class="tui-col-4 text-df text-white" @click='infoTab'>个人资料<uni-icons :size="20"
								class="padding-right" color="#FFF" type="arrowright" /></view>
					</view>
				</uni-list-item>
				<uni-list-item @tap="goSetting">
					<view><b>设置</b></view>
				</uni-list-item>
				<uni-list-item @tap="goVersion">
					<view><b>当前版本：<a>{{ 'V'+version }}</a></b></view>
				</uni-list-item>
			</uni-list>
		</view>
		<view v-else><uni-card style="border: none;border-radius: 2rpx;" :isFull="true">欢迎使用移动OM办公系统，请登录……</uni-card>
		</view>
		<button class="om-bottom bg-brown om-btn-color" v-if="!hasLogin" @tap="bindLogin">登录</button>
	</om-page>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js';
	export default {
		data() {
			return {
				logoColors: this.CuColorList,
				version: ''
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['hasLogin', 'forcedLogin', 'userName', 'userId', "avatar", "sexCode"
				// #ifdef MP-WEIXIN
				, 'db', 'openid', 'orgCode'
				// #endif
			])
		},
		methods: {
			...mapActions(useLoginUserStore, ['logout'
				// #ifdef MP-WEIXIN
				, 'setOrgCode', 'setSubUr', 'setUserId'
				// #endif
			]),
			goSetting() {
				uni.navigateTo({
					url: "./setting"
				})
			},
			bindLogin() {
				uni.reLaunch({
					url: '../login/login/login'
				});
			},
			infoTab() {
				uni.navigateTo({
					url: "./info"
				})
				// this.om.getPageInfoByTableMark('KEY_31', this.userId, this.userName)
			}
			// #ifdef H5 || APP-PLUS
			,
			bindLogout() {
				let _that = this;
				_that.om.request('loginCenter/h5Logout').then(res => {
					if (res.succeed) {
						_that.logout();
						_that.om.clearToken();
						if (_that.forcedLogin) {
							uni.reLaunch({
								url: '../login/login/login'
							});
						}
					}
				});
			}
			// #endif
			// #ifdef MP-WEIXIN
			,
			bindLogout() {
				let _that = this
				_that.om.request('wxlogin/unbind', {
					reason: "退出登录"
				}, 'post').then(d => {
					if (d.succeed) {
						try {
							_that.db.collection('accounts').where({
								"_openid": _that.openid
							}).get().then(res => {
								if (res.data.length) {
									var d = res.data[0]
									if (d.orges.length > 1) {
										var orges = []
										for (var i = 0; i < d.orges.length; i++) {
											var org = d.orges[i]
											if (org.orgCode !== _that.orgCode) {
												orges.push(org)
											}
										}
										const _ = _that.db.command
										_that.db.collection('accounts').doc(d._id).update({
											// data 传入需要局部更新的数据
											data: {
												orges: _.set(orges)
											},
											success() {
												_that.setOrgCode("")
												_that.setSubUr("")
												_that.setUserId("")
												uni.reLaunch({
													url: '../login/login/login'
												});
											},
											fail: function(fail) {
												console.log(fail)
											}
										})
									} else {
										_that.db.collection('accounts').doc(res.data[0]._id).remove()
										_that.setOrgCode("")
										_that.setSubUr("")
										_that.setUserId("")
										uni.reLaunch({
											url: '../login/login/login'
										});
									}
								}
							})
						} catch (e) {
							console.error(e)
						}
					}
				})
			}
			// #endif
			,
			goVersion() {
				uni.navigateTo({
					url: '/pages/login/version-info/version-info'
				})
			}
		},
		onLoad() {
			this.version = this.$version
		}
	};
</script>

<style>
	.page {
		background-color: #e5e5e5;
		height: 100%;
	}

	.om-bottom {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 18px;
		line-height: 2.55555556;
		position: fixed;
		bottom: 150rpx;
		width: 80%;
		z-index: 100;
		border-top-color: #ddd;
		color: #ecf6fd;
		left: 10%;
	}
</style>